<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 用户列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="searchData.keywords" placeholder="筛选关键词" class="handle-input mr10"></el-input>
        <el-select v-model="searchData.department_id" placeholder="筛选部门" class="handle-select mr10">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item,index) in departData"
            :key="index"
          ></el-option>
        </el-select>
        <el-button type="primary" icon="search" @click="search">搜索</el-button>
        <el-button type="primary fr" :loading="downloadLoading" @click="exportExcel">批量导出</el-button>
        <el-button type="primary fr">导入</el-button>
      </div>
      <el-table
        :data="userData"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID" sortable width="70" align="center"></el-table-column>
        <el-table-column prop="date" label="标识" width="70" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名" width="170" align="center"></el-table-column>
        <el-table-column prop="department_name" label="部门" width="100" align="center"></el-table-column>
        <el-table-column prop="image_head_url" label="头像" width="100" align="center"></el-table-column>
        <el-table-column prop="sexName" label="性别" width="70" align="center"></el-table-column>
        <el-table-column prop="birth" label="生日" width="130" align="center"></el-table-column>
        <el-table-column prop="company" label="单位" align="center"></el-table-column>
        <el-table-column prop="create_time" label="添加时间" width="240" align="center"></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
import * as api from "@/api/userApi.js";
export default {
  name: 'basetable',
  data() {
    return {
      url: './static/vuetable.json',
      userData: [],
      departData: [], //部门数据,对应用户所在的部门
      total:0, //用户列表人数
      cur_page: 1,
      multipleSelection: [],
      searchData: {
        department_id: '',
        keywords: '',
      },
      is_search: false,
      idx: -1,
      downloadLoading: false
    }
  },
  created() {
    this.getOther();
  },
  methods: {
    // 分页导航
    handleCurrentChange(val) {
      console.log(val);
      this.cur_page = val;
      this.getData(val);
    },
    getData(page) {
      api.getUserList({
        page:page
      })
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.userData = res.list.data
          if(!page){
            this.total = res.total
          }
          console.log(res);
          this.userData.forEach(d => {
            this.departData.forEach(d1 => {
              if (d1.id == d.department_id) {
                d.department_name = d1.name
              }
            })
            if (d.status == 0) {
              d.status = true
            } else {
              d.status = false
            }
            if (d.sex == 0) {
              d.sexName = '男'
            } else {
              d.sexName = '女'
            }
          })
        })

    },
    async getOther() {
      await api.getDepartList()
        .then((res) => {
          this.departData = res.data.list
          console.log(res, '部门');
        })
      await this.getData();
    },
    search() {
      this.is_search = true;
      console.log(this.searchData);
      api.getUserList(this.searchData)
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.userData = res.list.data
          console.log(res);
        })
    },
    exportExcel() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['id', 'date','name', 'department_name', 'image_head_url', 'sexName','birth','company','create_time']
        const filterVal = ['id', 'date','name', 'department_name', 'image_head_url', 'sexName','birth','company','create_time']
        const list = this.userData
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader, //Header Required
          data, //Specific data Required
          filename: 'excel-list', //Optional
          autoWidth: true, //Optional
          bookType: 'xlsx' //Optional
        })
        this.downloadLoading = false
      })
    },
    search() {
      this.is_search = true;
      api.getUserList({
        keywords: this.select_word
      })
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.userData = res.list.data
          console.log(res);
        })
    },
    formatter(row, column) {
      return row.address;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}

</script>

<style scoped>
.userTable-info {
  font-size: 16px;
}
.handle-box {
  margin-bottom: 10px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.el-row {
  margin-bottom: 10px;
}
.s-right {
  margin-right: 20px;
}
.fr {
  float: right;
}
</style>
